function CommodityRequireCommentSet() {
    this.commodityRequireId = 0;
    this.comments = {};
    this.masterCommentIdArraySortByReleaseTime = [];
}

CommodityRequireCommentSet.InitByCommodityRequireId = function (commodityRequireId, callback) {
    var commentSet = new CommodityRequireCommentSet;
    commentSet.commodityRequireId = commodityRequireId;

    function sortCommentIdByReleaseTime(a, b) {
        if (commentSet.comments[a].releaseTime > commentSet.comments[b].releaseTime) return 1;
        else if (commentSet.comments[a].releaseTime < commentSet.comments[b].releaseTime) return -1;
        else return 0;
    }

    function getDescendantComments(comments, commentId) {
        var descendantCommentIdArray = comments[commentId].childrenCommentIdArray;
        comments[commentId].childrenCommentIdArray.forEach(function (commentId) {
            descendantCommentIdArray = descendantCommentIdArray.concat(getDescendantComments(comments, commentId));
        });
        return descendantCommentIdArray;
    }

    $.ajax({
        url: base_url('api/getCommodityRequireComment'),
        type: 'post',
        data: {commodity_require_id: commodityRequireId},
        success: function (jsonrsp) {
            try {
                var rsp = $.parseJSON(jsonrsp);
                switch (rsp.type) {
                    case 'success':
                        rsp.data.forEach(function (data) {
                            var comment = CommodityRequireComment.InitByJsonData(data);
                            commentSet.comments[comment.id] = comment;
                            if (comment.fatherId == 0)
                                commentSet.masterCommentIdArraySortByReleaseTime.push(comment.id);
                        });

                        $.each(commentSet.comments, function (i, comment) {
                            var fatherComment = commentSet.comments[comment.fatherId];
                            if (!fatherComment) {
                                fatherComment = new CommodityRequireComment;
                            }
                            fatherComment.childrenCommentIdArray.push(comment.id);
                        });

                        $.each(commentSet.comments, function (i, comment) {
                            comment.descendantCommentIdArray = getDescendantComments(commentSet.comments, comment.id);
                            comment.descendantCommentIdArraySortByReleaseTime = comment.descendantCommentIdArray.concat().sort(sortCommentIdByReleaseTime);
                        });

                        commentSet.masterCommentIdArraySortByReleaseTime.sort(sortCommentIdByReleaseTime);

                        if (callback) callback(commentSet);
                        break;
                    default:
                        break;
                }
            } catch (ex) {
                noty({type: 'error', text: '发生内部错误，请联系管理员'});
                throw ex;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest, textStatus, errorThrown);
            noty({type: 'error', text: '发生内部错误，请联系管理员'});
        }
    });
};